<template>
  <el-upload
    ref="upload"
    :action="action"
    :before-upload="beforeUpload"
    class="picture-uploader"
    :disabled="disabled"
    :headers="headers"
    :limit="1"
    :multiple="false"
    :name="name"
    :on-error="onError"
    :on-exceed="onExceed"
    :on-remove="onRemove"
    :on-success="onSuccess"
    show-file-list
  >
    <img v-if="imageUrl" class="picture" :src="imageUrl" />
    <i v-else class="el-icon-plus picture-uploader-icon" />
  </el-upload>
</template>

<script>
  import { token } from '@/utils/constant'
  export default {
    name: 'ImgUpload',
    props: {
      name: {
        type: String,
        default: 'company',
      },
      fileList: {
        type: Array,
        default() {
          return []
        },
      },
      disabled: {
        type: Boolean,
        default: false,
      },
      imageUrl: {
        type: String,
        default: '',
      },
    },
    data() {
      return {
        action: `http://api.zesi.com.cn/api/upload`,
      }
    },
    computed: {
      headers() {
        return {
          token,
        }
      },
    },
    methods: {
      onRemove() {
        this.$message({
          message: '文件删除成功',
          type: 'success',
        })
        // eslint-disable-next-line vue/no-mutating-props
        this.imageUrl = ''
        this.$emit('onRemove')
      },
      onExceed() {
        this.$message({
          message: '每次只能上传一个文件',
          type: 'warning',
        })
      },
      beforeUpload(file) {
        this.$emit('beforeUpload', file)
      },
      onSuccess(response, file) {
        console.log('onSuccess', response, file)
        const { errorCode, msg, data } = response
        if (errorCode !== 0) {
          this.$message({
            message: (msg && `上传失败，失败原因：${msg}`) || '上传失败',
            type: 'error',
          })
          this.$emit('onError', data)
        } else {
          this.$message({
            message: '上传成功',
            type: 'success',
          })
          // eslint-disable-next-line vue/no-mutating-props
          this.imageUrl = URL.createObjectURL(file.raw)
          this.$refs.upload.clearFiles()
          this.$emit('onSuccess', data)
        }
      },
      onError(err) {
        const errMsg = (err.message && JSON.parse(err.message)) || '上传失败'
        this.$message({
          message:
            (errMsg.msg && `上传失败，失败原因：${errMsg.msg}`) || '上传失败',
          type: 'error',
        })
        this.$emit('onError', err)
      },
    },
  }
</script>

<style lang="scss" scoped>
  .picture-uploader {
    margin-bottom: 0;
  }

  .picture-uploader .el-upload {
    position: relative;
    overflow: hidden;
    cursor: pointer;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
  }

  .picture-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .picture-uploader-icon {
    width: 64px;
    height: 64px;
    font-size: 28px;
    line-height: 64px;
    color: #8c939d;
    text-align: center;
    border: 1px dashed #ddd;
    border-radius: 4px;
  }

  .picture {
    display: block;
    width: 64px;
    height: 64px;
    border-radius: 4px;
  }
</style>
